package com.zretc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import com.google.gson.Gson;
import com.zretc.dao.ZhiweiDao;
import com.zretc.entity.Zhiwei;
import com.zretc.impl.ZhiweiDaoImpl;

import com.zretc.service.ZhiweiService;
import com.zretc.serviceimpl.ZhiweiServiceImpl;
import com.zretc.tools.AjaxResponse;
import com.zretc.tools.PageData;

/**
 * Servlet implementation class ZhiweiServlet
 */
@WebServlet("/ZhiweiServlet")
public class ZhiweiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ZhiweiDao zd = new ZhiweiDaoImpl();
	ZhiweiService zs = new ZhiweiServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ZhiweiServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if ("add".equals(op)) {
			// ������͵Ĳ���
			String bumen = request.getParameter("bumen");
			String name = request.getParameter("name");
			String didian = request.getParameter("didian");
			String xuqiu = request.getParameter("xuqiu");
			int salary = Integer.parseInt(request.getParameter("salary"));
			String neirong = request.getParameter("neirong");

			// �ظ��ύ -III ������һ���ж��Ƿ�Ϊ�ظ��ύ�Ĳ���
			String tokenSession = (String) request.getSession().getAttribute("token");
			String tokenRequest = request.getParameter("token");

			// �ظ��ύ���ж�
			if (tokenSession == null || tokenRequest == null || !tokenSession.equals(tokenRequest)) {
				// �ظ��ύ��1
				request.setAttribute("msg", "重复提交了！请勿刷新！");
				request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
			} else {
				Zhiwei zhiwei = new Zhiwei(bumen, name, didian, xuqiu, salary, neirong);
				boolean flag = zd.addZhiwei(zhiwei);

				request.setAttribute("msg", flag == true ? "添加成功" : "添加失败");
				request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
			}
		}
		if ("".equals(op)) {
			String keywords = "";
			if (request.getParameter("keywords") != null) {
				keywords = request.getParameter("keywords");
			}
			// 查询 将结果存储在属性范围内，转发到显示页面去

			List<Zhiwei> list = zs.getZhiwei(keywords);

			request.setAttribute("list", list);

			// 转发
			request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
		}
		if ("del".equals(op)) {
			response.setContentType("appliction/json;charset=utf-8");
			int gid = 0;
			if (request.getParameter("gid") != null) {
				gid = Integer.parseInt(request.getParameter("gid"));

			}
			boolean flag = zs.delByid(gid);
			AjaxResponse res = new AjaxResponse(flag + "", 200);
			Gson gson = new Gson();
			String jsonString = gson.toJson(res);

			// 4 响应
			PrintWriter outPrintWriter = response.getWriter();
			outPrintWriter.print(jsonString);

			// 5.关闭
			outPrintWriter.close();
		}
		if ("queryByPage".equals(op)) {
			String keywords = "";
			// 2获取请求参数
			if (request.getParameter("keywords") != null) {
				keywords = request.getParameter("keywords");
			}
			// 页码默认值为1
			int page = 1;
			// 每页记录数默认为10
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}

			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));
			}

			// 3 调用 Service的方法
			PageData<Zhiwei> pd = zs.getUsersByPage(page, pageSize, keywords);

			// 4传递PageData对象给jsp，此时传递的是pageData的对象
			request.setAttribute("pd", pd);

			// 4.-1转发回来的时候，我们将查询关键字也存储在request范围内
			request.setAttribute("keywords", keywords);

			// 5 转发
			request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
		}

		if ("mohu".equals(op)) {
			doJianli(request, response);
		}
		if ("updata".equals(op)) {
			doupdata(request, response);
		}
		
	}

	private void doupdata(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String bumen = request.getParameter("ssbumen");
		String name = request.getParameter("ssname");
		String didian = request.getParameter("ssdidian");
		String xuqiu = request.getParameter("ssxuqiu");
		int salary = Integer.parseInt(request.getParameter("sssalary"));
		String neirong = request.getParameter("ssneirong");
		int gid = Integer.parseInt(request.getParameter("ssgid"));
		System.out.println(gid + bumen + name);
		Zhiwei zhiwei = new Zhiwei(bumen, name, didian, xuqiu, salary, neirong, gid);
		boolean flag = zd.updatazhiwei(zhiwei);
		if (flag) {
			JOptionPane.showMessageDialog(null, "修改成功");
			request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
		} else {
			JOptionPane.showMessageDialog(null, "修改失败");
			request.getRequestDispatcher("modalDetail.jsp").forward(request, response);
		}

	}

	private void doJianli(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String keyword = request.getParameter("keyword");
		System.out.println("keyword:" + keyword);
		List<Zhiwei> list2 = zd.getZhiwei(keyword);
		request.setAttribute("list2", list2);
		request.getRequestDispatcher("zhiwei.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
